package com.lyz.myself.service.ServiceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lyz.myself.config.utils.util.DateUtil;
import com.lyz.myself.controller.BaseController;
import com.lyz.myself.mapper.*;
import com.lyz.myself.pojo.Po.Business;
import com.lyz.myself.pojo.Po.RBusinessFile;
import com.lyz.myself.pojo.Vo.BusinessProductVo;
import com.lyz.myself.service.BusinessService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @program: myself
 * @Date: 2019/1/26 10:51
 * @Author: Mr.Deng
 * @Description:
 */
@Service
public class BusinessServiceImpl extends BaseController implements BusinessService {

    @Autowired
    BusinessMapper businessMapper;

    @Autowired
    private RBusinessProductMapper rBusinessProductMapper;

    @Autowired
    BusinessDataMapper businessDataMapper;

    @Autowired
    FileMapper fileMapper;

    @Autowired
    RBusinessFileMapper rBusinessFileMapper;

    /**
     * 添加商机
     *
     * @mbg.generated 2019-01-26
     */
    @Transactional
    @Override
    public boolean insertBusiness(BusinessProductVo list) {
        list.getBusiness().setCompany_id(getUser().getCompany_id());
        list.getBusiness().setCreator_role_id(getUser().getRole_id());
        list.getBusiness().setCreate_time(DateUtil.timeMillis());
        list.getBusiness().setUpdate_time(DateUtil.timeMillis());
        list.getBusiness().setTotal_amount(0);
        list.getBusiness().setSubtotal_val(0.0);
        list.getBusiness().setDiscount_price(0.0);
        list.getBusiness().setSales_price(0.0);
        list.getBusiness().setDue_date(0);
        list.getBusiness().setUpdate_role_id(0);
        list.getBusiness().setIs_deleted(0);
        list.getBusiness().setDelete_role_id(0);
        list.getBusiness().setDelete_time(0);
        int business_id = businessMapper.insertBusiness(list.getBusiness());
        System.out.println(list.getBusiness().getBusiness_id());
        list.getBusinessData().setBusiness_id(list.getBusiness().getBusiness_id());
        list.getBusinessData().setCompany_id(list.getBusiness().getCompany_id());
        int bussinessData = businessDataMapper.insertSelective(list.getBusinessData());
        boolean boo;
        int insertProduct = 0;
        if (list.getProductList() != null) {
            for (int i = 0; i < list.getProductList().size(); i++) {
                list.getProductList().get(i).setBusiness_id(list.getBusiness().getBusiness_id());
                if (list.getProductList().get(i).getDiscount_rate() == null || list.getProductList().get(i).getDiscount_rate().equals("")) {
                    list.getProductList().get(i).setDiscount_rate(0);
                }
                if (list.getProductList().get(i).getTax_rate() == null || list.getProductList().get(i).getTax_rate().equals("")) {
                    list.getProductList().get(i).setTax_rate(0);
                }
                if (list.getProductList().get(i).getDescription() == null || list.getProductList().get(i).getDescription().equals("")) {
                    list.getProductList().get(i).setDescription("");
                }
                list.getProductList().get(i).setCompany_id(getUser().getCompany_id());
            }
            insertProduct = rBusinessProductMapper.insertSelective(list.getProductList());
        }
        int insertFile = 0;
        int insertBuFile = 0;
        List<RBusinessFile> listBuFile = new ArrayList<>();
        if (list.getFileLeads().size() > 0) {
            for (int i = 0; i < list.getFileLeads().size(); i++) {
                list.getFileLeads().get(i).setBelongs(1);
                list.getFileLeads().get(i).setCompany_id(list.getBusiness().getCompany_id());
                list.getFileLeads().get(i).setCreate_date(DateUtil.timeMillis());
                list.getFileLeads().get(i).setRole_id(getUser().getRole_id());
            }
            insertFile = fileMapper.insert(list.getFileLeads());
            for (int i = 0; i < list.getFileLeads().size(); i++) {
                RBusinessFile rBusinessFile = new RBusinessFile();
                rBusinessFile.setBusiness_id(list.getBusiness().getBusiness_id());
                rBusinessFile.setCompany_id(list.getBusiness().getCompany_id());
                rBusinessFile.setFile_id(list.getFileLeads().get(i).getFile_id());
                listBuFile.add(rBusinessFile);
            }
            insertBuFile = rBusinessFileMapper.insert(listBuFile);
        }
        if (business_id > 0 && bussinessData > 0 || insertProduct > 0 || insertFile > 0 || insertBuFile > 0) {
            boo = true;
        } else {
            boo = false;
        }
        return boo;
    }

    /**
     * 查询我负责的所有商机
     *
     * @param map
     * @return
     */
    @Override
    public PageInfo<Map<String, Object>> selectBusiness(Map<String, Object> map) {
        map.put("company_id", getUser().getCompany_id());
        map.put("user_id", getUser().getUser_id());
        PageHelper.startPage((Integer) map.get("pageNum"), (Integer) map.get("pageSize"));
        List<Map<String, Object>> userList = businessMapper.selectBusiness(map);
        for (int i = 0; i < userList.size(); i++) {
            if (userList.get(i).get("creator_role_id") == getUser().getUser_id()) {
                userList.get(i).put("icreate", 0);
            } else {
                userList.get(i).put("icreate", "");
            }
            if (userList.get(i).get("owner_role_id") == getUser().getUser_id()) {
                userList.get(i).put("iowner", 0);
            } else {
                userList.get(i).put("iowner", "");
            }
        }
        PageInfo<Map<String, Object>> page = new PageInfo<>(userList);
        return page;
    }

    /**
     *根据id查看商机信息
     * @mbg.generated 2019-01-26
     */
    public List<Map<String, String>> selectByPrimaryKey(Integer business_id){
        return businessMapper.selectByPrimaryKey(business_id);
    }

}
